package com.nbjtjc.safe.service.emg;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.activiti.mapper.ExtActNodeMapper;
import com.nbjtjc.safe.activiti.model.ActivitiParams;
import com.nbjtjc.safe.activiti.model.ExtActNode;
import com.nbjtjc.safe.activiti.model.MyTask;
import com.nbjtjc.safe.activiti.service.ActivitiService;
import com.nbjtjc.safe.activiti.service.EmgFlowService;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.job.service.ScheduleBusinessJobService;
import com.nbjtjc.safe.mapper.emg.*;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.mapper.sys.SysNoticeMapper;
import com.nbjtjc.safe.mapper.sys.SysNoticeTargetMapper;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.EmgPlanPublicGoodsHistory;
import com.nbjtjc.safe.model.emg.*;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class EmgPlanPublicService {

    @Autowired
    private EmgPlanPublicMapper emgPlanPublicMapper;

    @Autowired
    private EmgPlanMapper emgPlanMapper;

    @Autowired
    private EmgPlanPublicGoodsMapper emgPlanPublicGoodsMapper;

    @Autowired
    private EmgDutyMapper emgDutyMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EmgPlanGoodsMapper emgPlanGoodsMapper;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private SysNoticeTargetMapper sysNoticeTargetMapper;
    @Autowired
    private EmgGoodsMapper emgGoodsMapper;
    @Autowired
    private EmgPlanPublicGoodsHistoryMapper emgPlanPublicGoodsHistoryMapper;

    @Autowired
    private EmgPlanPublicWriteTargetMapper emgPlanPublicWriteTargetMapper;

    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private ExtActNodeMapper extActNodeMapper;
    @Autowired
    private SysDeptService sysDeptService;

    @Lazy
    @Autowired
    private EmgFlowService emgFlowService;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private EmgPlanPublicResponseService emgPlanPublicResponseService;

    @Autowired
    private HistoryService historyService;
    @Autowired
    private ActivitiService activitiService;

    @Autowired
    private ScheduleBusinessJobService scheduleBusinessJobService;

    @Transactional(rollbackFor = {Throwable.class})
    public Result delete(int emgPlanPublicId) {
        Result result = new Result();
        EmgPlanPublic emgPlanPublic = emgPlanPublicMapper.selectByPrimaryKey(emgPlanPublicId);
        if (ObjectUtil.isNotNull(emgPlanPublic)) {
            if (StringUtils.isNotEmpty(emgPlanPublic.getProcessInstanceId())) {
                try {
                    activitiService.deleteProcessInstance(emgPlanPublic.getProcessInstanceId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        emgDutyMapper.deleteByPlanPublicId(emgPlanPublicId);
        emgPlanPublicGoodsMapper.deleteByPlanPublicId(emgPlanPublicId);
        emgPlanPublicMapper.deleteByPrimaryKey(emgPlanPublicId);
        return result;
    }

    /**
     * 查找母公司的应急响应
     *
     * @param emgPlanPublic
     * @param page
     * @return
     */
    public Result findMotherCompanyEmg(EmgPlanPublic emgPlanPublic, Page page) {
        Integer motherCompanyId = sysDeptService.getParentCompanyId(MyShiroUtils.getCompanyId());
        emgPlanPublic.setCompanyId(motherCompanyId);
        Result<PageInfo> result = find(emgPlanPublic, page);
        emgPlanPublicResponseService.compareEmgPlanPublicResponseStatus(result.getData().getList());
        return result;
    }

    public Result<PageInfo> find(EmgPlanPublic emgPlanPublic, Page page) {
        Result result = new Result();
        try {
            PageHelper.startPage(page.getPageCurrent(), page.getPageSize());
            //TODO  发布通知和解除通知可以剥离开来
            List<EmgPlanPublic> emgPlanPublicList = emgPlanPublicMapper.search(emgPlanPublic, page);
//            comparePlanPublicNoticeTargets(emgPlanPublicList);
            compareEmgPlanPublicStatus(emgPlanPublicList);
            result.setData(new PageInfo<>(emgPlanPublicList));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Transactional
    public Result republicEmgPlan(EmgPlanPublic emgPlanPublic, Map<String, Object> params) throws Exception {
        Result result = add(emgPlanPublic, true);
        //previousId是被调整的emgPlanPublicId
        emgPlanPublicMapper.setHistory(Integer.parseInt(params.get("previousId").toString()));
        params.put("result", 0);
        ActivitiParams activitiParams = new ActivitiParams();
        activitiParams.setResult(0);
        activitiParams.setTaskId(emgPlanPublic.getTaskId());
        emgFlowService.finishTask(activitiParams);
        //  调整发布时，检查对应的定时任务，更新执行时间的间隔
        scheduleBusinessJobService.updateEmgWorkBriefExecuteDate
                (emgPlanPublic.getStartPlanId(), emgPlanPublic.getTimeInterval());
        return result;
    }

    /**
     * @param emgPlanPublic
     * @param republicEmgPlan 是否为调整发布
     * @return
     */
    @Transactional(rollbackFor = {Throwable.class})
    public Result add(EmgPlanPublic emgPlanPublic, boolean republicEmgPlan) {
        Result result = new Result();
        int res = 0;
        if (ObjectUtil.isNull(emgPlanPublic)) {
            result.setFalse("不能为空，预案模板的id");
            return result;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        //当时间间隔没有时，设置默认时间间隔为2
        if (ObjectUtil.isNull(emgPlanPublic.getTimeInterval())
                || emgPlanPublic.getTimeInterval() <= 0) {
            emgPlanPublic.setTimeInterval(2);
        }
        emgPlanPublic.setCreateUserId(sysUser.getUserId());
        emgPlanPublic.setCreateDate(new Date());

        res = emgPlanPublicMapper.insertSelective(emgPlanPublic);
        if (res == 0) {
            result.setFalse("插入失败");
            return result;
        }

        //设置起始的id
        if (ObjectUtil.isNull(emgPlanPublic) || ObjectUtil.isNull(emgPlanPublic.getStartPlanId())
                || emgPlanPublic.getStartPlanId().equals(0)) {
            emgPlanPublic.setStartPlanId(emgPlanPublic.getId());
        }
        //TODO 转发的启动id 设置，不是空的话就不用更新
        if (!republicEmgPlan) {
            emgDutyMapper.deleteByPlanPublicId(emgPlanPublic.getStartPlanId());
            emgPlanPublicGoodsMapper.deleteByPlanPublicId(emgPlanPublic.getStartPlanId());
        }
        emgPlanPublicMapper.updateByPrimaryKeySelective(emgPlanPublic);
        compareEmgGoods2PlanPublic(emgPlanPublic);
        insertNotice(emgPlanPublic);
        if (!republicEmgPlan) {
            emgPlanPublicResponseService.addEmgPlanPublicResponse(emgPlanPublic);
            insertPlanPublicDuty(emgPlanPublic);
            insertPlanPublicGoods(emgPlanPublic);
        }
        result.setData(emgPlanPublic);
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result update(EmgPlanPublic emgPlanPublic) throws Exception {
        Result result = new Result();
        int res = 0;
        if (ObjectUtil.isNull(emgPlanPublic)) {
            result.setFalse("不能为空 ");
            return result;
        }

        compareEmgGoods2PlanPublic(emgPlanPublic);
        List<EmgPlanPublicGoods> emgPlanPublicGoodsList = emgPlanPublic.getEmgPlanPublicGoodsList();

        emgPlanPublic.setEmgDuties(emgDutyMapper
                .findByPlanPublicId(emgPlanPublic.getStartPlanId()));


//        emgDutyMapper.deleteByPlanPublicId(emgPlanPublic.getStartPlanId());
        emgPlanPublicGoodsMapper.deleteByPlanPublicId(emgPlanPublic.getStartPlanId());
        res = emgPlanPublicMapper.updateByPrimaryKeySelective(emgPlanPublic);
        if (res == 0) {
            throw new Exception("更新发布计划失败");
        }
        emgPlanPublic = emgPlanPublicMapper.selectByPrimaryKey(emgPlanPublic.getId());
        emgPlanPublic.setEmgPlanPublicGoodsList(emgPlanPublicGoodsList);
//        insertPlanPublicDuty(emgPlanPublic);
        insertPlanPublicGoods(emgPlanPublic);
        finishPlanPublic(emgPlanPublic);
        result.setData(emgPlanPublic);
        return result;
    }


    /**
     * 解除应急，并发布通知
     *
     * @param emgPlanPublic
     * @param sysNotice
     * @return
     */
    public Result relievePlanPublic(EmgPlanPublic emgPlanPublic, SysNotice sysNotice) {
        Result result = new Result();
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isNull(emgPlanPublic) || ObjectUtil.isNull(emgPlanPublic.getId())) {
            result.setFalse("不能为空");
            return result;
        }
        //查找发布通知的用户id 作为解除通知的通知对象
        SysNotice publicNotice = sysNoticeMapper.findByPlanPublicId(emgPlanPublic.getId());
        if (ObjectUtil.isNotNull(publicNotice)) {
            List<Integer> userIds = sysNoticeTargetMapper.findUserIdsByNoticeId(publicNotice.getnId());
            emgPlanPublic.setNoticeUserIds(userIds);
        }
        int res = sysNoticeMapper.deletePlanPublicRelieveNotice(emgPlanPublic.getId());
        sysNotice.setWriteUserId(MyShiroUtils.getSessionUserId());
        sysNotice.setDeptId(MyShiroUtils.getDeptId());
        sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
        sysNotice.setTitle("应急响应通知");
        sysNotice.setCreateTime(new Date());
        sysNotice.setRelateId(emgPlanPublic.getId());
        sysNotice.setNoticeStatus(Constant.SysNoticeStatus.SAVE.getValue());
        sysNotice.setIdType(Constant.SysNoticeType.EMG_PLAN_RELIEVE.getValue());
        sysNotice.setNoticeType(Constant.SysNoticeType.EMG_PLAN_RELIEVE.getValue());
        sysNoticeService.addNoticeList(sysNotice, emgPlanPublic.getNoticeUserIds());
        //更新解除时间
        emgPlanPublic.setRelieveTime(new Date());
        emgPlanPublicMapper.updateByPrimaryKeySelective(emgPlanPublic);
        map.put("planPublic", emgPlanPublic);
        map.put("notice", sysNotice);
        result.setData(map);
        return result;
    }

    /**
     * 有emgPlanId的话，就将预案模板中的物资添加到要发布的物资中去
     *
     * @param emgPlanPublic
     */
    private void compareEmgGoods2PlanPublic(EmgPlanPublic emgPlanPublic) {
        List<EmgPlanPublicGoods> emgPlanPublicGoodsList = new ArrayList<>();

        if (null != emgPlanPublic.getStartPlanId()) ;
        {
            emgPlanPublicGoodsList = emgPlanPublicGoodsMapper
                    .findByPlanPublicId(emgPlanPublic.getStartPlanId());
        }

        if (CollectionUtil.isNotEmpty(emgPlanPublicGoodsList)) {
            emgPlanPublic.getEmgPlanPublicGoodsList().addAll(emgPlanPublicGoodsList);
        }
        if (ObjectUtil.isNotNull(emgPlanPublic.getPlanId())) {
            List<EmgPlanGoods> emgPlanGoods = emgPlanGoodsMapper
                    .findByPlanId(emgPlanPublic.getPlanId());
            if (CollectionUtil.isNotEmpty(emgPlanGoods)) {
                Set<String> goodsName = emgPlanPublic.getEmgPlanPublicGoodsList()
                        .stream().map(EmgPlanPublicGoods::getName).collect(Collectors.toSet());
                emgPlanGoods.forEach(planGoods -> {
                    if(!goodsName.contains(planGoods.getName())){
                        EmgPlanPublicGoods emgPlanPublicGoods = new EmgPlanPublicGoods();
                        emgPlanPublicGoods.setPlanPublicId(emgPlanPublic.getStartPlanId());
                        emgPlanPublicGoods.setName(planGoods.getName());
                        emgPlanPublic.getEmgPlanPublicGoodsList().add(emgPlanPublicGoods);
                    }
                });
            }
//            emgPlanPublic.setTimeInterval(emgPlan.getTimeInterval());
        }
    }

    /**
     * 当发布的应急响应需要结束时，需要将物资保存到历史记录中去
     *
     * @param emgPlanPublic
     */
    private void finishPlanPublic(EmgPlanPublic emgPlanPublic) {

        if (emgPlanPublic.getFinish()) {
//            List<String> emgGoodsNames = emgPlan.getEmgPlanGoods().stream()
//                    .map(EmgPlanGoods::getName).collect(Collectors.toList());
            List<String> emgGoodsNames = emgPlanPublicGoodsMapper.findGoodsNameByPlanPublicId(emgPlanPublic.getStartPlanId());

            if (CollectionUtil.isEmpty(emgGoodsNames)) {
                return;
            }
            List<EmgGoods> emgGoodsList = new ArrayList<>();
            EmgGoods emgGoods = new EmgGoods();
            emgGoodsNames.forEach(emgGoodsName -> {
                emgGoods.setGoodsName(emgGoodsName);
                emgGoodsList.addAll(emgGoodsMapper.search(emgGoods, new Page()));
            });
            if (CollectionUtil.isEmpty(emgGoodsList)) {
                return;
            }
            List<EmgPlanPublicGoodsHistory> emgPlanPublicGoodsHistories = new ArrayList<>();
            emgGoodsList.forEach(goods -> {
                EmgPlanPublicGoodsHistory emgPlanPublicGoodsHistory = new EmgPlanPublicGoodsHistory();
                emgPlanPublicGoodsHistory.setGoodsName(goods.getGoodsName());
                emgPlanPublicGoodsHistory.setCount(goods.getCount());
                emgPlanPublicGoodsHistory.setDept(goods.getDeptName());
                emgPlanPublicGoodsHistory.setManagername(goods.getManageUser());
                emgPlanPublicGoodsHistory.setLocation(goods.getGoodsLocation());
                emgPlanPublicGoodsHistory.setPhone(goods.getContact());
                emgPlanPublicGoodsHistory.setEmgPlanPublicId(emgPlanPublic.getId());
                emgPlanPublicGoodsHistories.add(emgPlanPublicGoodsHistory);
            });
            emgPlanPublicGoodsHistoryMapper.insertList(emgPlanPublicGoodsHistories);
        }
    }

    /**
     * 插入发布预案关联的物资
     *
     * @param emgPlanPublic
     */
    private void insertPlanPublicGoods(EmgPlanPublic emgPlanPublic) {
        if (CollectionUtil.isNotEmpty(emgPlanPublic.getEmgPlanPublicGoodsList())) {
            emgPlanPublicGoodsMapper.insertList(emgPlanPublic.getEmgPlanPublicGoodsList());
        }
    }

    /**
     * 插入发布预案的
     *
     * @param emgPlanPublic
     */
    private void insertPlanPublicDuty(EmgPlanPublic emgPlanPublic) {
        if (CollectionUtil.isNotEmpty(emgPlanPublic.getEmgDuties())) {
            String companyName = sysDeptMapper.findNameById(emgPlanPublic.getCompanyId());
            emgPlanPublic.getEmgDuties().forEach(emgDuty -> {
                if (ObjectUtil.isNotNull(emgDuty.getUserId()) && emgDuty.getUserId() > 0) {
                    SysUser sysUser = sysUserMapper.selectByUserId(emgDuty.getUserId());
                    emgDuty.setName(sysUser.getUsername());
                    emgDuty.setPhone(sysUser.getMobile());
                    emgDuty.setShortPhone(sysUser.getShortPhone());
                }
                emgDuty.setCompany(companyName);
                emgDuty.setCompanyId(emgPlanPublic.getCompanyId());
                emgDuty.setPlanPublicId(emgPlanPublic.getStartPlanId());
            });
        }
        if (CollectionUtil.isNotEmpty(emgPlanPublic.getEmgDuties())) {
            emgDutyMapper.insertList(emgPlanPublic.getEmgDuties());
        }
    }

    /**
     * 插入需要填写简报的下级单位
     *
     * @param emgPlanPublic
     */
    private void insertPlanPublicWriteTarget(EmgPlanPublic emgPlanPublic) {
        if (CollectionUtil.isNotEmpty(emgPlanPublic.getWriteTargets())) {
            emgPlanPublic.getWriteTargets().forEach(writeTarget -> {
                writeTarget.setPlanPublicId(emgPlanPublic.getStartPlanId());
            });
            emgPlanPublicWriteTargetMapper.insertList(emgPlanPublic.getWriteTargets());
        }
    }

    /**
     * 发布对应的通知
     *
     * @param emgPlanPublic
     */
    private void insertNotice(EmgPlanPublic emgPlanPublic) {
//        if (CollectionUtil.isNotEmpty(emgPlanPublic.getNoticeUserIds())) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setWriteUserId(MyShiroUtils.getSessionUserId());
        sysNotice.setDeptId(MyShiroUtils.getDeptId());
        sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
        sysNotice.setTitle("应急通知--" + emgPlanPublic.getName());
        sysNotice.setCreateTime(new Date());
        sysNotice.setContent(emgPlanPublic.getStartInfo());
        sysNotice.setRelateId(emgPlanPublic.getId());
        sysNotice.setNoticeStatus(Constant.SysNoticeStatus.SAVE.getValue());
        sysNotice.setIdType(Constant.SysNoticeType.EMG_PLAN_PUBLIC.getValue());
        sysNotice.setNoticeType(Constant.SysNoticeType.EMG_PLAN_PUBLIC.getValue());
//            sysNoticeService.addNoticeList(sysNotice, emgPlanPublic.getNoticeUserIds());
        sysNoticeService.addNotice(sysNotice, MyShiroUtils.getCompanyId());
//        }
    }

    public Map getPlanPublicSummaryInitInfo(int planPublicId, int startPlanPublicId) {
        Map<String, Object> initInfo = new HashMap<>();
        EmgPlanPublic endPlan = emgPlanPublicMapper.selectByPrimaryKey(planPublicId);
        EmgPlanPublic startPlan = emgPlanPublicMapper.selectByPrimaryKey(startPlanPublicId);
        if (ObjectUtil.isNull(endPlan) || ObjectUtil.isNull(startPlan)) {
            return initInfo;
        }
        String timeRange = DateUtil.format(startPlan.getCreateDate(), "yyyy-MM-dd hh:mm")
                + "至" + DateUtil.format(endPlan.getRelieveTime(), "yyyy-MM-dd hh:mm");
        initInfo.put("timeRange", timeRange);
        return initInfo;
    }


    /**
     * find接口时，将通知对象的id带上
     *
     * @param emgPlanPublics
     */
    private void comparePlanPublicNoticeTargets(List<EmgPlanPublic> emgPlanPublics) {
        if (CollectionUtil.isEmpty(emgPlanPublics)) {
            return;
        }
        //todo fix bug
        emgPlanPublics.forEach(emgPlanPublic -> {
            Integer maxSysNoticeId = sysNoticeMapper
                    .findLatestSysNoticeIdByPlanPublicId(emgPlanPublic.getId());
            if (ObjectUtil.isNotNull(maxSysNoticeId)) {
                List<Integer> noticeUserIds = sysNoticeTargetMapper.findUserIdsByNoticeId(maxSysNoticeId);
                emgPlanPublic.setNoticeUserIds(noticeUserIds);
            }
        });
    }

    /**
     * 装配应急响应的状态
     *
     * @param emgPlanPublics
     */
    private void compareEmgPlanPublicStatus(List<EmgPlanPublic> emgPlanPublics) {
        if (CollectionUtil.isEmpty(emgPlanPublics)) {
            return;
        }
        emgPlanPublics.forEach(emgPlanPublic -> {
            if (ObjectUtil.isNotNull(emgPlanPublic.getProcessInstanceId())) {
                ProcessInstance pi = runtimeService.createProcessInstanceQuery()
                        .processInstanceId(emgPlanPublic.getProcessInstanceId()).singleResult();
                if (ObjectUtil.isNull(pi)) {
                    emgPlanPublic.setStatusName("结束响应");
                } else {
                    Task task = taskService.createTaskQuery()
                            .processInstanceId(emgPlanPublic.getProcessInstanceId()).singleResult();
                    if (ObjectUtil.isNotNull(task)) {
                        MyTask myTask = new MyTask();
                        myTask.setCreateTime(task.getCreateTime());
                        myTask.setProcessInstanceId(task.getProcessInstanceId());
                        myTask.setName(task.getName());
                        myTask.setId(task.getId());
                        myTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
                        myTask.setExecutionId(task.getExecutionId());
                        emgPlanPublic.setTask(myTask);
                        ExtActNode extActNode = extActNodeMapper
                                .findByProcdefIdAndNodeId(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
                        if (ObjectUtil.isNotNull(extActNode)) {
                            emgPlanPublic.setStatusName(extActNode.getName());
                            emgPlanPublic.setExtActNode(extActNode);
                            emgPlanPublic.setStatus(extActNode.getStatus());
                        }
                    }
                }
            }
        });
    }

    /**
     * 当节点执行到
     */
    public void updateEmgPlanPublicNoticeStatus(int planPublicId) {
        sysNoticeMapper.updateNoticeTarget(planPublicId, Constant.SysNoticeType.EMG_PLAN_PUBLIC.getValue());
    }


    /**
     * 获取应急响应的其他信息，子公司的响应信息，值班信息
     *
     * @param planPublicId
     * @return
     */
    public Map<String, Object> getEmgPlanInfo(int planPublicId) {
        //TODO
        Map<String, Object> datas = new HashMap<>();
        SysDept sysDept = new SysDept();
        sysDept.setParentId(MyShiroUtils.getCompanyId());
        sysDept.setType(Constant.SysDeptType.COMPANY.getValue());
        List<SysDept> sysDepts = sysDeptMapper.select(sysDept);
        if (CollectionUtil.isNotEmpty(sysDepts)) {
            Map<String, Object> dutyMap = new HashMap<>();
            sysDepts.forEach(company -> {
                EmgDuty emgDuty = new EmgDuty();
                emgDuty.setCompanyId(company.getId());
                emgDuty.setPlanPublicId(planPublicId);
                List<EmgDuty> emgDuties = emgDutyMapper.select(emgDuty);
                dutyMap.put(company.getName()
                        , CollectionUtil.isEmpty(emgDuties) ? new ArrayList<>() : emgDuties);
            });
            datas.put("subCompanyDuties", dutyMap);
        }
        EmgPlanPublicResponse emgPlanPublicResponse = new EmgPlanPublicResponse();
        emgPlanPublicResponse.setEmgPlanPulicId(planPublicId);
        List<EmgPlanPublicResponse> emgPlanPublicResponses =
                emgPlanPublicResponseService.find(emgPlanPublicResponse);
        datas.put("emgPlanPublicResponses", emgPlanPublicResponses);
        return datas;
    }


}
